//
//  searchDetailViewController.m
//  UI_HLJ_Frame
//
//  Created by  毕宏伟 on 15/7/8.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "searchDetailViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "MysearchCollectionViewCell.h"
#import "AFNetworking.h"
#import "HTTPTool.h"
#import "SearchPageModel.h"
#import "contentModel.h"
#import "itemstwoModel.h"
#import "contenttwoModel.h"

@interface searchDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)UIView *backView;
@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)NSMutableArray *onePageArr;
@property(nonatomic,retain)NSMutableArray *twoPageArr;
@property(nonatomic,retain)NSMutableArray *contentArr;
@property(nonatomic,retain)NSMutableArray *contenttwoArr;
@property(nonatomic,retain)UISegmentedControl *segmentcontrol;



@end

@implementation searchDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor cyanColor];
    
    [self createView];
    [self createData];

}

- (void)createData
{
    self.onePageArr =[NSMutableArray array];
    self.twoPageArr =[NSMutableArray array];
    self.contentArr =[NSMutableArray array];
    self.contenttwoArr =[NSMutableArray array];
    NSDictionary *bodydic =@{@"type":@"1",@"citycode":@"0",@"searchtxt":self.nameurl,@"pagecount":@"20",@"offset":@"0",@"sorttype":@"1"};
    [HTTPTool post:@"http://hunliji.com/p/wedding/index.php/home/APISearch" body:bodydic httpResult:JSON requestBodyStyle:BodyJSON success:^(id result) {
        
        NSDictionary *dic =result;
        NSArray *itemsArr =dic[@"items"];
        for (NSDictionary *itemsdic in itemsArr) {
            SearchPageModel *se =[[SearchPageModel alloc]init];
            [se setValuesForKeysWithDictionary:itemsdic];
            [self.onePageArr addObject:se];
            [se release];
            
            NSDictionary *contentdic =itemsdic[@"content"];
            contentModel *con =[[contentModel alloc]init];
            [con setValuesForKeysWithDictionary:contentdic];
            [self.contentArr addObject:con];
            [con release];
            
        }

        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    NSDictionary *dic1 =@{@"type":@"2",@"citycode":@"0",@"searchtxt":self.nameurl,@"pagecount":@"20",@"offset":@"0",@"sorttype":@"1"};
    
    [HTTPTool post:@"http://hunliji.com/p/wedding/index.php/home/APISearch" body:dic1 httpResult:JSON requestBodyStyle:BodyJSON success:^(id result) {
        
        NSDictionary *dic =result;
        NSArray *itemsArr =dic[@"items"];
        for (NSDictionary *itemsdic in itemsArr) {
            
            itemstwoModel *it =[[itemstwoModel alloc]init];
            [it setValuesForKeysWithDictionary:itemsdic];
            [self.twoPageArr addObject:it];
            [it release];
            
            NSDictionary *contentdic =itemsdic[@"content"];
            
            contenttwoModel *contwo =[[contenttwoModel alloc]init];
            [contwo setValuesForKeysWithDictionary:contentdic];
            [self.contenttwoArr addObject:contwo];
            [contwo release];
        }
        
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
        
        
    }];

    
    
}


- (void)createView
{
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    self.backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.backView];
    
    
    
    //创建collectionview相关
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize =CGSizeMake(WIDTH, HEIGHT);
    flow.minimumInteritemSpacing =0;
    flow.minimumLineSpacing =0;
    flow.scrollDirection =UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT) collectionViewLayout:flow];
    self.collectionView.backgroundColor =[UIColor greenColor];
    self.collectionView.delegate =self;
    self.collectionView.dataSource =self;
    self.collectionView.pagingEnabled=YES;
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    
    //collectionView的注册实现
    [self.collectionView registerClass:[MysearchCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    NSArray *arr =@[@"套餐",@"案例",];
    //创建segment
    self.segmentcontrol =[[UISegmentedControl alloc]initWithItems:arr];
    self.segmentcontrol.selectedSegmentIndex =0;
    self.segmentcontrol.frame =CGRectMake(self.backView.frame.size.width/3, self.backView.frame.size.height/3, self.backView.frame.size.width/3, self.backView.frame.size.height/2);
    [self.view addSubview:self.segmentcontrol];
    [self.segmentcontrol addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
    [_segmentcontrol release];
    
    
}


- (void)click:(UISegmentedControl *)seg
{
    if (self.segmentcontrol.selectedSegmentIndex==0) {
        
        self.collectionView.contentOffset =CGPointMake(0, 0);
    }
    else
    {
        self.collectionView.contentOffset =CGPointMake(WIDTH, 0);
    }
}

#pragma mark 实现collectionView的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MysearchCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    if (indexPath.row ==0) {
        cell.onePageArr =self.onePageArr;
        cell.flag =indexPath.row;
        cell.contentArr =self.contentArr;
        [cell.tableView reloadData];
        
    }
    else
    {
        cell.twoPageArr =self.twoPageArr;
        cell.flag=indexPath.row;
        cell.contenttwoArr =self.contenttwoArr;
        [cell.tableView reloadData];
        
    }
    
    
    return cell;
}





- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.collectionView.contentOffset.x ==0) {
        
        self.segmentcontrol.selectedSegmentIndex =0;
        
    }
    else
    {
        self.segmentcontrol.selectedSegmentIndex =1;
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
